package com.sq.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateTimeUtil {

    public static Map getWeekTime(int n){
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map map = new HashMap();
        //获取本周时间
        String s_time = getTimeInterval(0).split(",")[1]; //本周周日最后时间
        String e_time = getTimeInterval(n).split(",")[0]; //前2周周一开始时间
        Date startdate = null;
        Date endDate = null;
        try {
            startdate = df.parse(e_time);
            endDate = df.parse(s_time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        map.put("s_time",startdate);
        map.put("e_time",endDate);
        return map;
    }
    // 获得当天0点时间
    public static Date getTimesmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // 获得某天中午12点时间
    public static Date getTimesAfternoon(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 12);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    // 获得某天24点时间
    public static Date getTimesNight(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    // 获得某天0点时间
    public static Date getTimesMorning(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    // 获得当天24点时间
    public static Date getTimesnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    // 获得昨天0点时间
    public static Date getYesterdaymorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // 获得昨天24点时间
    public static Date getYesterdaynight() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // 获得本周一0点时间
    public static Date getTimesWeekmorning() {
////        Calendar cal = Calendar.getInstance();
////        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
////        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
////        return cal.getTime();
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    // 获得本周日24点时间
    public static Date getTimesWeeknight() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getTimesWeekmorning());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
////        Calendar cal = Calendar.getInstance();
////        cal.setTime(getTimesWeekmorning());
////        cal.add(Calendar.DAY_OF_WEEK, 7);
////        return cal.getTime();
    }
    // 获得本月第一天0点时间
    public static Date getTimesMonthmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    // 获得本月最后一天24点时间
    public static Date getTimesMonthnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return cal.getTime();
    }
    // 获得上月第一天0点时间
    public static Date getLastMonthmorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    // 获得上月最后一天24点时间
    public static Date getLastMonthnight() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return cal.getTime();
    }

    //获取本年的最早时间
    public static  Date getFirstTimeOfOneYear(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.MONTH,cal.getActualMinimum(Calendar.MONTH));
        return cal.getTime();
    }

    //获取本年的最后时间
    public static Date getLastTimeOfOneYear(){
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR) +1, cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.MONTH,cal.getActualMinimum(Calendar.MONTH));
        return cal.getTime();
    }

    /**
     * 根据当前日期获得n周前的日期区间（n周周一和n周日日期）
     * @param n
     * @return
     */
    public static String getTimeInterval(int n) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if(1 == dayWeek) {
            calendar1.add(Calendar.DAY_OF_WEEK, -1);
            calendar2.add(Calendar.DAY_OF_WEEK, -1);
        }
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek-7*(n-1);
        int offset2 = 7 - dayOfWeek-7*(n-1);
        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.MILLISECOND, 0);
        String lastBeginDate = sdf.format(calendar1.getTime());
        calendar2.set(Calendar.HOUR_OF_DAY, 24);
        calendar2.set(Calendar.SECOND, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.MILLISECOND, 0);
        String lastEndDate = sdf.format(calendar2.getTime());
        return lastBeginDate + "," + lastEndDate;
    }
    /**
     * 日期转星期
     *
     * @param datetime
     * @return
     */
    public static String dateToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }
    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate,Date bdate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 获取当天的日期
     * @return
     */
    public static String getToday(){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(new Date());
    }
    public static void main(String[] args) {

        Date firstOfOneYear = DateTimeUtil.getFirstTimeOfOneYear();//本年最早的时间
        long sTime = firstOfOneYear.getTime()/1000;
        System.out.println(sTime);
//        Map weekTime = getWeekTime(0);
//        long sTime = (long) weekTime.get("s_time");
//        long eTime = (long) weekTime.get("e_time");
//        System.out.println(sTime);
//        System.out.println(eTime);
       /* SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date timesAfternoon = getTimesAfternoon(new Date(2018 - 06 - 05));

        System.out.println(timesAfternoon);*/
//        String[] arr = getTimeInterval(-1).split(",");
//        Date dt = null;
//        try {
//            dt = df.parse(arr[0]);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        long time  = dt.getTime();
//        System.out.println(time/1000);
//        System.out.println(getTimeInterval(-1));
//        System.out.println(df.format(getTimesWeekmorning()));
//        Map map = new HashMap();
//        map = getWeekTime(2);
//        System.out.println(map);
//        System.out.println(dateToWeek("2017-11-08"));

//        System.out.println(getTimesWeekmorning().getTime()/1000);
//        System.out.println(getTimesWeeknight().getTime()/1000);

//       getTimeIntervalp(df);
//        System.out.println(getTimesWeekmorning().getTime()/1000);
//        System.out.println(getTimesWeeknight().getTime()/1000);

//        System.out.println(getTimeInterval(0));
//        System.out.println(getTimeInterval(1));
//        System.out.println(getTimeInterval(2));
//
//        System.out.println(getBeforeWeekTime(0));
//        System.out.println(getBeforeWeekTime(1));
//        System.out.println(getBeforeWeekTime(2));
//        System.out.println(getBeforeWeekTime(1));
//        System.out.println(getYesterdaymorning().getTime()/1000);
//        System.out.println(getYesterdaynight().getTime()/1000);
//        System.out.println(getLastMonthmorning().getTime()/1000);
//        System.out.println(getLastMonthnight().getTime()/1000);

    }
    /**
     * 计算当前时间和之前时间相差的天数
     * @return 相差天数
     * @throws ParseException
     */
    public  static  int getDifferenceDays(Long reviweTime){
         Long nowTime = System.currentTimeMillis() / 1000; //精确到秒
         Float f=  nowTime.floatValue();
        int round = Math.round((f - reviweTime) / 3600 / 24);
        return  round;
    }
    /**
     * 计算近几周前的时间（几周前的当前时间）
     * @return 周数
     * @throws ParseException
     */
    public  static  Date getNowWeekBefore(int i){
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c=Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE,-(i*7));
        Date date=c.getTime();
        // String dateTime=dateFormat.format(date);
        return  date;
    }

    /**
     * 计算几周前的时间（n周前周一到周日）
     * @return 周数
     * @throws ParseException
     */
    public  static Map getBeforeWeekTime(int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if(1 == dayWeek) {
            calendar1.add(Calendar.DAY_OF_WEEK, -1);
            calendar2.add(Calendar.DAY_OF_WEEK, -1);
        }
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek-7*(n-1);
        int offset2 = 7 - dayOfWeek-7*(n-1);
        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.MILLISECOND, 0);
        Date startTime = calendar1.getTime();
        calendar2.add(Calendar.DATE, offset2 - 7);
        calendar2.set(Calendar.HOUR_OF_DAY, 24);
        calendar2.set(Calendar.SECOND, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.MILLISECOND, 0);
        Date endTime = calendar2.getTime();
        Map  map =new HashMap();
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        return  map;
    }

    //获取某个日期的开始时间
   public static Date getDayStartTime(Date d) {
       Calendar calendar = Calendar.getInstance();
       if(null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
         calendar.set(Calendar.MILLISECOND, 0);
         return calendar.getTime();
//         return new Timestamp(calendar.getTimeInMillis());
       }
    //获取某个日期的结束时间
    public static Date getDayEndTime(Date d) {
            Calendar calendar = Calendar.getInstance();
           if(null != d) calendar.setTime(d);
              calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH)+1, 0, 0, 0);
          calendar.set(Calendar.MILLISECOND, 999);
            return calendar.getTime();
    }

    public static String getMonthEnglish() {
        Map map = new HashMap();
        map.put("01", "january");
        map.put("02", "february");
        map.put("03", "march");
        map.put("04", "april");
        map.put("05", "may");
        map.put("06", "june");
        map.put("07", "july");
        map.put("08", "august");
        map.put("09", "september");
        map.put("10", "october");
        map.put("11", "november");
        map.put("12", "december");
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        String month = sdf.format(new Date());
        return map.get(month).toString();
    }

    /**
     * 极速那时间为time的时候，前后多少天的时间
     *
     * @param day  例如（填写 -1 ，为计算昨天0点0分0秒的时间，填写 1，为计算明天24点0分0秒的时间），不可传0
     * @return
     */
    public static Long getTimeForDay(Integer day) {
        if (day == 0) {
            return 0l;
        }
        int seconds = day * 24 * 60 * 60;
        if (seconds < 0) {//时间往前设置
            //获取当天0点时间
            Date timesmorning = getTimesmorning();
            long morningtime = timesmorning.getTime() / 1000;
            return morningtime + seconds;
        }else {
            Date timesmorning = getTimesmorning();
            long morningtime = timesmorning.getTime() / 1000;

            return morningtime + seconds + 24 * 60 * 60;
        }

    }


    public static String getSpecifiedDayBefore(String specifiedDay){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c = Calendar.getInstance();

        Date date=null;

        try {

            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);

        } catch (ParseException e) {

            e.printStackTrace();

        }

        c.setTime(date); int day=c.get(Calendar.DATE);

        c.set(Calendar.DATE,day-1);

        String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());

        return dayBefore;

    }

}

